<link href="/css/style.css" rel="stylesheet">
<link href="/css/style-responsive.css" rel="stylesheet">
<link href="/js/advanced-datatable/css/demo_page.css" rel="stylesheet"/>
<link href="/js/advanced-datatable/css/demo_table.css" rel="stylesheet"/>
<link rel="stylesheet" href="/js/data-tables/DT_bootstrap.css"/>
<link href="/css/style.css" rel="stylesheet">
<link href="/css/style-responsive.css" rel="stylesheet">
<link href="/css/select2.css" rel="stylesheet">

<body class="panel" style="padding: 0;">
<div class="row">
    <div class="col-sm-12">

        <div class="panel-body">
            <header class="panel-heading" style="height: 50px;">
                <div class="col-md-12" align="middle"> 推送消息到客户端</div>
            </header>
        </div>

        <div class="panel-body">
            <form class="form-horizontal">

                <div class="form-group">
                    <label class="col-sm-2 control-label">客户端系统</label>
                    <div class="col-sm-5">
                        <select id="system" name="system" class="select2" data-placeholder="选择系统..." onchange="changeSystem()">
                            <option value="0">iphone</option>
                            <option value="1">android</option>
                            <option value="2">ipad</option>
                            <option value="3">android pad</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">客户端版本号</label>
                    <div class="col-sm-5">
                        <div class="btn-group mr5" style="width: 90px">
                            <select id="appverStart" name="appverStart" class="select2" data-placeholder="选择版本号...">

                            </select>
                        </div>
                        <div class="btn-group mr5">
                            -
                        </div>
                        <div class="btn-group mr5" style="width: 90px">
                            <select id="appverEnd" name="appverEnd" class="select2" data-placeholder="选择版本号...">

                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">客户端的包名</label>
                    <div class="col-sm-5">
                        <select id="bundleId" name="bundleId" class="select2" multiple data-placeholder="选择包名...">

                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">系统的版本号</label>
                    <div class="col-sm-5">
                        <div class="btn-group mr5" style="width: 90px">
                            <select id="osversionStart" name="osversionStart" class="select2" data-placeholder="选择版本号...">

                            </select>
                        </div>
                        <div class="btn-group mr5">
                            -
                        </div>
                        <div class="btn-group mr5" style="width: 90px">
                            <select id="osversionEnd" name="osversionEnd" class="select2" data-placeholder="选择版本号...">

                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2  control-label">手机厂商名</label>
                    <div class="col-sm-5">
                        <select id="brand" name="brand" class="select2" multiple data-placeholder="选择手机厂商名...">

                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">手机型号</label>
                    <div class="col-sm-5">
                        <select id="model" name="model" class="select2" multiple data-placeholder="选择手机型号...">

                        </select>
                    </div>
                </div>


                <div class="form-group">
                    <label class="col-sm-2 control-label">选择客户端用户文件</label>
                    <div class="col-sm-10">
                        <input type="file" id="useridFile" name="useridFile" onChange="onUseridFileChange()" />
                        <p class="help-block" style="font-size: 5px">
                            (如需全员推送请上传文件中写"*"号)
                        </p>
                    </div>

                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">推送消息内容</label>
                    <div class="col-sm-10">
                        <textarea style="width: 95%;height: 30%;" id="eventContentInput" name="eventContentInput"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2  control-label"></label>
                    <div class="col-sm-10">
                        <button type="button" class="btn btn-success btn-sm" id="uploadupvdataButton" disabled="disabled"
                                onclick="uploadUPVData()">确定</button>
                    </div>
                </div>
            </form>
        </div>

    </div>
</div>

<!-- Placed js at the end of the document so the pages load faster -->
<script src="/js/jquery-1.10.2.min.js"></script>
<script src="/js/jquery-ui-1.9.2.custom.min.js"></script>
<script src="/js/jquery-migrate-1.2.1.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/modernizr.min.js"></script>
<script src="/js/jquery.nicescroll.js"></script>
<!--common scripts for all pages-->
<script src="/js/scripts.js"></script>
<script src="/js/html5shiv.js"></script>
<script src="/js/respond.min.js"></script>
<script src="/js/jquery.ajaxfileupload.js"></script>
<script src="/js/ajaxfileupload.js"></script>
<script src="/js/select2.min.js"></script>
<script>

    jQuery(document).ready(function(){
        // Select2
        jQuery(".select2").select2({
            width: '100%'
        });
    });


    //select 各个值
    var appverStart
    var appverEnd
    var bundleId
    var system
    var osversionStart
    var osversionEnd
    var brand
    var model
    var system  //选择平台
    var currentListData = new Array();

    function getList(system) {
        $.get(host + "/list_basic_info_all/system/" + system, null, listToSuccess, "json");
    }
    //从数据库中获取列表成功
    function listToSuccess(response, status, xhr) {
        if (status == "success" && response.reusltCode == "200") {
            currentListData = response.reusltDetail;
            totalCount = response.totalCount;
            clearDataTable();
            fillSelect();
            parent.setMainContentHeight(currentListData.length, 70);
        } else {
            alert(response);
        }
    }

    //清空数据表格
    function clearDataTable() {
        appverStart = $("#appverStart");
        appverStart.empty();
        appverStart.select2({ width: '100%', val : "" });

        appverEnd = $("#appverEnd");
        appverEnd.empty()
        appverEnd.select2({ width: '100%', val : "" });

        bundleId = $("#bundleId");
        bundleId.empty()
        bundleId.val(null).trigger("change");

        system = $("#system");

        osversionStart = $("#osversionStart");
        osversionStart.empty()
        osversionStart.select2({ width: '100%', val : "" });

        osversionEnd = $("#osversionEnd");
        osversionEnd.empty()
        osversionEnd.select2({ width: '100%', val : "" });

        brand = $("#brand");
        brand.empty()
        brand.val(null).trigger("change");

        model = $("#model");
        model.empty()
        model.val(null).trigger("change");

    }

    //添加选择类型
    function fillSelect() {
        //手机端策略，系统对应值必须是这些值
        var systemValue = {
            "Iphone": 0,
            "Android": 1,
            "Ipad": 2,
            "AndroidPad":3,
        }

        //组装select值
        adddata = {}; // Map map = new HashMap();
        adddata["appver"] += ("<option value='*'>无限制</option>");
        adddata["osversion"] += ("<option value='*'>无限制</option>");
        $.each(currentListData, function(index, item) {
            switch(item.type)
            {
                case "appver":
                    adddata["appver"] += ("<option value=\""+ item.value +"\" >" + item.value + "</option>");
                    break;
                case "bundleId":
                    adddata["bundleId"] += ("<option value=\""+ item.value +"\" >" + item.value + "</option>");
                    break;
//                case "system":
//                    adddata["system"] += ("<option value="+ systemValue[item.value] +" >" + item.value + "</option>");
//                    break;
                case "osversion":
                    adddata["osversion"] += ("<option value=\""+ item.value +"\" >" + item.value + "</option>");
                    break;
                case "brand":
                    adddata["brand"] += ("<option value=\""+ item.value +"\" >" + item.value + "</option>");
                    break;
                case "model":
                    adddata["model"] += ("<option value=\""+ item.value +"\" >" + item.value + "</option>");
                    break;
                default:
                    //其他类型不处理，直接排除
            }
        });
        appverStart.append(adddata["appver"]);
        appverEnd.append(adddata["appver"]);
        bundleId.append(adddata["bundleId"]);
//        system.append(adddata["system"]);
        osversionStart.append(adddata["osversion"]);
        osversionEnd.append(adddata["osversion"]);
        brand.append(adddata["brand"]);
        model.append(adddata["model"]);
    }



    var totalCount = 0
    var currentListData
    var adddata
    var host

    //页面载入后先获取默认条数的数据
    window.onload = function () {
        host = "http://" + window.location.host;
        getList(0);
    }
    //从数据库中获取列表成功
    function onUseridFileChange() {

        if (document.getElementById("useridFile").value) {
            var extList = document.getElementById("useridFile").value.split(".");
            var ext = extList[extList.length - 1];
            if (!(/^(txt|ini)$/.test(ext))) {
                alert("uid串必须是用英文逗号间隔的txt或者ini文档");
                return false;
            }
        }
        document.getElementById('uploadupvdataButton').disabled = "";
    }

    function onBodyResize() {

    }
    function uploadUPVData() {

//        Major_Version_Number.Minor_Version_Number[.Revision_Number[.Build_Number]]
//        主版本号            .子版本号              [.修正版本号       [.编译版本号  ]]

        /**
         * 版本比较 VersionCompare
         * @param {String} currVer 当前版本.
         * @param {String} promoteVer 比较版本.
         * @return {Boolean} false 当前版本小于比较版本返回 true.
         *
         * 使用
         * VersionCompare("6.3","5.2.5"); // false.
         * VersionCompare("6.1", "6.1"); // false.
         * VersionCompare("6.1.5", "6.2"); // true.
         */
        var VersionCompare = function (currVer, promoteVer) {
            currVer = currVer || "0.0.0";
            promoteVer = promoteVer || "0.0.0";
            if (currVer == promoteVer) return false;
            var currVerArr = currVer.split(".");
            var promoteVerArr = promoteVer.split(".");
            var len = Math.max(currVerArr.length, promoteVerArr.length);
            for (var i = 0; i < len; i++) {
                var proVal = ~~promoteVerArr[i],
                        curVal = ~~currVerArr[i];
                if (proVal < curVal) {
                    return false;
                } else if (proVal > curVal) {
                    return true;
                }
            }
            return false;
        };

        appverStartVal = appverStart.val()
        appverEndVal = appverEnd.val()
        if (appverStartVal != "*" && appverEndVal != "*" && VersionCompare(appverEndVal, appverStartVal)) {
            //如果开始版本大于结束版本
            alert("客户端版本号开始版本大于结束版本");
            return false;
        }
        osversionStartVal = osversionStart.val()
        osversionEndVal = osversionEnd.val()
        if (osversionStartVal != "*" && osversionEndVal != "*" && VersionCompare(osversionEndVal, osversionStartVal)) {
            //如果开始版本大于结束版本
            alert("系统版本号开始版本大于结束版本");
            return false;
        }



        // 开始上传文件时显示一个图片
        $("#showBusyImg2").ajaxStart(function () {
            $(this).show();
            // 文件上传完成将图片隐藏起来
        }).ajaxComplete(function () {
            $(this).hide();
        });


        var data = {
            eventContentInput: document.getElementById("eventContentInput").value,
            typeValue: 2,

            //过滤条件
            appverStart: appverStartVal,
            appverEnd: appverEndVal,
            bundleId: bundleId.val(),
            system: $("#system").val(),
            osversionStart : osversionStartVal,
            osversionEnd : osversionEndVal,
            brand : brand.val(),
            model : model.val(),
            
        };

        if (confirm("是否确认推送消息？")) {
            $.ajaxFileUpload({
                url: '/upload_pushuids_file',
                type: 'post',
                secureuri: false, //一般设置为false
                fileElementId: 'useridFile', // 上传文件的id、name属性名
                data: data,
                dataType: 'json', //返回值类型，一般设置为json、application/json
                //	elementIds: elementIds, //传递参数到服务器
                success: function (response, status) {
                    if (status == "success") {
                        alert(response);
                    }
                },
                error: function (data, status, e)//服务器响应失败处理函数
                {
                    alert(e);
                    // parent.location.reload();
                }
            });
        }

        //return false;
    }

    
    function changeSystem() {
        clearDataTable();
        system = $("#system").val();
        if (system != "" ) {
            system = parseInt(system);
            getList(system);
        }else {
            currentListData = new Array();
            fillSelect();
        }
    }





</script>

</body>